package com.szsh.aiot.framework.config;

import com.szsh.aiot.hsm.dto.ability.AbilityRequest;
import com.szsh.aiot.hsm.dto.ability.NotifyOrderRequest;
import com.szsh.aiot.hsm.utils.RandomUtil;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;

import java.util.UUID;

/**
 * 能力总线接口配置
 *
 * @author zhangtt
 *
 */
@Component
@ConfigurationProperties(prefix = "hsm.ability")
public class AbilityParam {

    /** get请求url统一配置 */
    private String provinceID = "10001";
    private String appID = "1000000983224517";
    private String secret = "09f2a84de930c38d";

    private String aesSecret = "EcAaJM3ZJpwx4RnSqShXuA==";
    /** 头部信息X-APP-ID */
    private String headerAppID = "2fab432527254f7fa78e16f4f0577f7a";
    /** 头部信息X-APP-KEY */
    private String headerAppKey = "b3b4d6d0c8bd45e2b3ee6fe249083240";
    /** 总线接口地址 */
    private String endpoint = "http://192.168.197.108:38006/ebp/HNCMP";
    /** 总线接口测试地址 */
    private String testUrl = "http://192.168.196.71:38006/ebp/HNCMP";
    /** b01业务生产总线地址 */
    private String secendpoint = "http://192.168.197.108:38006/ebp/GameSpeedServerSystem";
    /** 增值平台B01业务密钥 */
    private String notifyCallSecret = "5JMKGSQpjoeJukRc9pNc";
    /** 增值平台B01业务调用方ID */
    private String notifyCallID = "523416862474";
    /** 增值平台B01业务业务标识 */
    private String notifyServiceType = "game_speeder";
    /** B01安全业务数据上报周期 */
    private int notifyPeriod = 100;
    /** B01安全管家采集平台地址 */
    private String notifyAddr = "mk-collect.189smarthome.com:7890";
    /** B01订阅接口 安全业务虚拟省份名 */
    private String areaName = "app_daily_upload";

    /** 订购反馈接口请求url */
    private String salesAcceptEndpoint = "http://192.168.197.108:38006/ebp/acceptPlatform/zjOrderFeedBack/zjOrderFeedBack";
    /** 判断是否天翼设备请求url */
    private String terminalEndpoint = "http://192.168.197.108:38006/ebp/dataCenter/getResidentialTerminalByMac/TerminalManageByMac/";
    /** APP聚合接口请求url */
    private String appinfoEndpoint = "http://192.168.197.108:38006/ebp/dataCenterQuery/yyrbDataSearch/dataSearch";

    private String reportUrl = "http://192.168.197.108:38006/ebp/acceptPlatform/acceptRightsReport/rightsReport";

    public String getAreaName() {
        return areaName;
    }

    public void setAreaName(String areaName) {
        this.areaName = areaName;
    }

    public String getProvinceID() {
        return provinceID;
    }

    public void setProvinceID(String provinceID) {
        this.provinceID = provinceID;
    }

    public String getAppID() {
        return appID;
    }

    public void setAppID(String appID) {
        this.appID = appID;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getEndpoint() {
        return endpoint;
    }

    public void setEndpoint(String endpoint) {
        this.endpoint = endpoint;
    }

    public String getTestUrl() {
        return testUrl;
    }

    public void setTestUrl(String testUrl) {
        this.testUrl = testUrl;
    }

    public String getSecendpoint() {
        return secendpoint;
    }

    public void setSecendpoint(String secendpoint) {
        this.secendpoint = secendpoint;
    }

    public String getHeaderAppID() {
        return headerAppID;
    }

    public void setHeaderAppID(String headerAppID) {
        this.headerAppID = headerAppID;
    }

    public String getHeaderAppKey() {
        return headerAppKey;
    }

    public void setHeaderAppKey(String headerAppKey) {
        this.headerAppKey = headerAppKey;
    }

    public String getNotifyCallSecret() {
        return notifyCallSecret;
    }

    public void setNotifyCallSecret(String notifyCallSecret) {
        this.notifyCallSecret = notifyCallSecret;
    }

    public String getNotifyCallID() {
        return notifyCallID;
    }

    public void setNotifyCallID(String notifyCallID) {
        this.notifyCallID = notifyCallID;
    }

    public String getNotifyServiceType() {
        return notifyServiceType;
    }

    public void setNotifyServiceType(String notifyServiceType) {
        this.notifyServiceType = notifyServiceType;
    }

    public int getNotifyPeriod() {
        return notifyPeriod;
    }

    public void setNotifyPeriod(int notifyPeriod) {
        this.notifyPeriod = notifyPeriod;
    }

    public String getNotifyAddr() {
        return notifyAddr;
    }

    public void setNotifyAddr(String notifyAddr) {
        this.notifyAddr = notifyAddr;
    }

    public String getSalesAcceptEndpoint() {
        return salesAcceptEndpoint;
    }

    public void setSalesAcceptEndpoint(String salesAcceptEndpoint) {
        this.salesAcceptEndpoint = salesAcceptEndpoint;
    }

    public String getTerminalEndpoint() {
        return terminalEndpoint;
    }

    public void setTerminalEndpoint(String terminalEndpoint) {
        this.terminalEndpoint = terminalEndpoint;
    }

    public String getAppinfoEndpoint() {
        return appinfoEndpoint;
    }

    public void setAppinfoEndpoint(String appinfoEndpoint) {
        this.appinfoEndpoint = appinfoEndpoint;
    }

    public String getReportUrl() {
        return reportUrl;
    }

    public void setReportUrl(String reportUrl) {
        this.reportUrl = reportUrl;
    }

    public String getAesSecret() {
        return aesSecret;
    }

    public void setAesSecret(String aesSecret) {
        this.aesSecret = aesSecret;
    }

    /**
     * 获取get请求URL的模板
     *
     * @param abilityOperateID
     * @return
     */
    public String getUrlTemplate(String abilityOperateID) {
        String paramTemplate = "OperateID=" + abilityOperateID + "&ProvinceID=" + provinceID
                + "&AppID=" + appID + "&Secret=" + secret;
        return endpoint + "/" + abilityOperateID + "?" + paramTemplate + "&MAC={1}";
    }

    /**
     * 获取get请求URL的模板
     *
     * @param abilityOperateID
     * @return
     */
    public String ppoeUrl(String abilityOperateID,String province) {
        return getPostEndponint(abilityOperateID,province) + "&PPPOE={1}";
    }

    /**
     * 获取post请求地址
     *
     * @param abilityOperateID
     * @return
     */
    public String getPostEndponint(String abilityOperateID,String province) {
        String paramTemplate = "OperateID=" + abilityOperateID + "&ProvinceID=" + province
                + "&AppID=" + appID + "&Secret=" + secret;
        return endpoint + "/" + abilityOperateID + "?" + paramTemplate;
    }

    /**
     * 获取URL的模板
     *
     * @param abilityOperateID
     * @return
     */
    public String getTestUrlTemplate(String abilityOperateID) {
        String paramTemplate = "OperateID=" + abilityOperateID + "&ProvinceID=" + provinceID
                + "&AppID=" + appID + "&Secret=" + secret;
        return testUrl + "/" + abilityOperateID + "?" + paramTemplate + "&MAC={1}";
    }

    /**
     * 生成能力总线http请求头数据
     *
     * @return
     */
    public HttpHeaders getHttpHeaders() {
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json;UTF-8");
        headers.add("X-APP-ID", headerAppID);
        headers.add("X-APP-KEY", headerAppKey);
        headers.add("X-CTG-Request-Id", UUID.randomUUID().toString());
        return headers;
    }

    /**
     * 获取post请求公用的请求参数
     *
     * @return
     */
    public AbilityRequest getRequestData(String operateID, Object body) {
        AbilityRequest requestParam = new AbilityRequest();
        requestParam.setOperateID(operateID);
        requestParam.setProvinceID(provinceID);
        requestParam.setAppID(appID);
        requestParam.setSecret(secret);
        requestParam.setBody(body);
        return requestParam;
    }
    /**
     * 增值平台B01业务请求
     *
     * @return
     */
    public NotifyOrderRequest getRequestBody() {
        // 报文流水号,发起方生成,全局唯一
        String transactionid = RandomUtil.randomHexString(54);
        // 时间戳
        Long timestamp = System.currentTimeMillis();
        // 校验字段
        String sign = DigestUtils.md5DigestAsHex((transactionid + timestamp + notifyCallID + notifyCallSecret).getBytes());
        NotifyOrderRequest requestBody = new NotifyOrderRequest();
        requestBody.setTransactionid(transactionid);
        requestBody.setTimestamp(timestamp);
        requestBody.setCallid(notifyCallID);
        requestBody.setSign(sign);
        requestBody.setServicetype(notifyServiceType);
        return requestBody;
    }

}
